//
//  YMGuideLaunchView.m
//  YMMedical
//
//  Created by y s on 2023/7/13.
//

#import "YMGuideLaunchView.h"
#import <SDCycleScrollView.h>
#import "YMGuideCollectionViewCell.h"
#import "YMHomeService.h"

#define YMGuide_Had_Show_Key @"YMGuide_Had_Show_Key"

@interface YMGuideLaunchView ()<SDCycleScrollViewDelegate>

@property (nonatomic, strong) SDCycleScrollView *vCycle;
@property (nonatomic, strong) UIButton *btnStart;
@property (nonatomic, strong) NSArray *arrImage;

@property (nonatomic, copy) void(^finishBlock)(void);
@property (nonatomic, copy) NSString *idStr;

@end

@implementation YMGuideLaunchView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUI];
    }
    return self;
}

#pragma mark -- UI
- (void)setUI{
    self.backgroundColor = STheme.D9;
    [self addSubview:self.vCycle];
    [self addSubview:self.btnStart];
    
    self.vCycle.imageURLStringsGroup = self.arrImage;
    
    [self.btnStart mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo([SFitUtil fit:-44]);
        make.centerX.mas_equalTo(0);
        make.size.mas_equalTo([SFitUtil fitSize:345 height:49]);
    }];
}

+ (void)showFinishBlock:(void(^)(void))finishBlock{
    WEAK_SELF;
    [YMHomeService requestAppGuideDataSuccess:^(NSArray * _Nonnull arrData, NSString * _Nonnull idStr) {
        if (arrData != nil && arrData.count > 0 && idStr != nil && idStr.length > 0){
            if ([SLocalUtil hasKey:YMGuide_Had_Show_Key]){
                NSString *tempStr = [SLocalUtil stringForKey:YMGuide_Had_Show_Key];
                if ([tempStr isEqualToString:idStr]){
                    if (finishBlock){
                        finishBlock();
                    }
                }else{
                    [YMGuideLaunchView showViewIdStr:idStr arrData:arrData finishBlock:finishBlock];
                }
            }else{
                [YMGuideLaunchView showViewIdStr:idStr arrData:arrData finishBlock:finishBlock];
            }
        }else{
            if (finishBlock){
                finishBlock();
            }
        }
    } failure:^(NSString * _Nonnull message) {
        if (finishBlock){
            finishBlock();
        }
    }];
}

+ (void)showViewIdStr:(NSString *)idStr
              arrData:(NSArray *)arrData
          finishBlock:(void(^)(void))finishBlock{
    YMGuideLaunchView *guide = [[YMGuideLaunchView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    guide.idStr = idStr;
    guide.arrImage = arrData;
    guide.finishBlock = ^{
        if (finishBlock){
            finishBlock();
        }
    };
    UIWindow *window = [SThemeUtil keyWindow];
    [window addSubview:guide];
}

- (void)startClickAction{
    
    [SLocalUtil setString:[SDataUtil stringOf:_idStr valid:@""] forKey:YMGuide_Had_Show_Key];
    [self removeFromSuperview];
    if (self.finishBlock){
        self.finishBlock();
    }
}
- (void)setArrImage:(NSArray *)arrImage{
    _arrImage = arrImage;
    NSMutableArray *arrTemp = [[NSMutableArray alloc] init];
    for (NSDictionary *item in arrImage) {
        NSString *imageStr = [SDataUtil stringOf:item[@"url"]];
        if (imageStr != nil && imageStr.length > 0){
            [arrTemp addObject:imageStr];
        }
    }
    self.vCycle.imageURLStringsGroup = arrTemp;
    if (arrImage.count == 1){
        self.btnStart.hidden = NO;
    }
}


//SDCycleScrollViewDelegate
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didScrollToIndex:(NSInteger)index{
    self.btnStart.hidden = index==self.arrImage.count-1 ? NO:YES;
}
- (Class)customCollectionViewCellClassForCycleScrollView:(SDCycleScrollView *)view{
    return [YMGuideCollectionViewCell class];
}
- (void)setupCustomCell:(UICollectionViewCell *)cell forIndex:(NSInteger)index cycleScrollView:(SDCycleScrollView *)view{
    if (index < self.arrImage.count) {
        YMGuideCollectionViewCell *cellN = (YMGuideCollectionViewCell *)cell;
        cellN.dict = [SDataUtil dictOf:self.arrImage[index]];
    }
}

#pragma mark -- setter and getter
- (SDCycleScrollView *)vCycle{
    if (_vCycle == nil) {
        CGRect rect = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        _vCycle = [SDCycleScrollView cycleScrollViewWithFrame:rect delegate:self placeholderImage:[UIImage imageNamed:@" "]];
        _vCycle.autoScrollTimeInterval = 5.0f;
        _vCycle.infiniteLoop = NO;
        _vCycle.showPageControl = YES;
        _vCycle.autoScroll = NO;
        _vCycle.pageControlStyle = SDCycleScrollViewPageContolStyleClassic;
        _vCycle.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
        _vCycle.currentPageDotColor = STheme.D13;
        _vCycle.pageDotColor = [SUIUtil colorOf:0xE5E5E5];
        _vCycle.pageControlDotSize = [SFitUtil fitSize:12 height:12];
        _vCycle.backgroundColor = [SUIUtil colorOf:0xffffff];
        _vCycle.clipsToBounds = YES;
        _vCycle.pageControlBottomOffset = [SFitUtil fit:140];
    }
    return _vCycle;
}
- (UIButton *)btnStart{
    if (_btnStart == nil){
        _btnStart = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnStart setTitle:KLanguage(@"start_use_txt") forState:UIControlStateNormal];
        [_btnStart setTitleColor:STheme.D9 forState:UIControlStateNormal];
        [_btnStart addTarget:self action:@selector(startClickAction) forControlEvents:UIControlEventTouchUpInside];
        _btnStart.titleLabel.font = [SFitUtil fitBoldFont:16];
        _btnStart.layer.cornerRadius = [SFitUtil fit:8];
        _btnStart.backgroundColor = STheme.D13;
        _btnStart.clipsToBounds = YES;
        _btnStart.hidden = YES;
    }
    return _btnStart;
}


@end
